package com.wanghaixin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanghaixin.dao.ActivityMapper;
import com.wanghaixin.dto.ActivityDTO;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.ActivityExample;
import com.wanghaixin.pojo.ActivityWithBLOBs;
import com.wanghaixin.service.ActivityService;
import com.wanghaixin.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/3 13:14
 * @Description: 活动管理模块
 */
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public int addActivity(ActivityDTO activityDTO) {
        //2.创建ActivityWithBLOBs
        ActivityWithBLOBs record = new ActivityWithBLOBs();

            //3,调用工具类，将传递过来的数据复制到ActivityWithBLOBs 中
            BeanUtils.copyProperties(activityDTO,record);
            //4,解决字段不一致问题
        Long aLong = null;
        Long aLong1 = null;
        try {
            aLong = DateUtils.strToLong(activityDTO.getBeginTime());
            aLong1 = DateUtils.strToLong(activityDTO.getEndTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        record.setBeginTime(aLong);
            record.setEndTime(aLong1);

        //1,调用mapper中生成的insert方法，参数为ActivityWithBLOBs record，所以创建ActivityWithBLOBs
        int i = activityMapper.insertSelective(record);
        return i;
    }

    @Override
    public void delActivity(Long id) {
        int i = activityMapper.deleteByPrimaryKey(id);

    }

    @Override //修改和添加基本是一样的，只是调用不同的mapper层方法
    public int updateActivity(ActivityDTO activityDTO) {
        //2.创建ActivityWithBLOBs
        ActivityWithBLOBs record = new ActivityWithBLOBs();
        try {
            //3,调用工具类，将传递过来的数据复制到ActivityWithBLOBs 中
            BeanUtils.copyProperties(activityDTO,record);
            //4,解决字段不一致问题
            Long aLong = DateUtils.strToLong(activityDTO.getBeginTime());
            Long aLong1 = DateUtils.strToLong(activityDTO.getEndTime());
            record.setBeginTime(aLong);
            record.setEndTime(aLong1);
        }  catch (ParseException e) {
            e.printStackTrace();
        }
        //1,调用mapper中生成的insert方法，参数为ActivityWithBLOBs record，所以创建ActivityWithBLOBs
        //修改时根据主键来进行修改的
        int i = activityMapper.updateByPrimaryKeyWithBLOBs(record);
        return i;
    }

    @Override
    public ActivityDTO findById(Long id) {
        //根据id查询的结果时间类型是long类型，我们转化为字符串
        ActivityWithBLOBs activityWithBLOBs = activityMapper.selectByPrimaryKey(id);
        //创建ActivityDTO ，接收数据
        ActivityDTO activityDTO = new ActivityDTO();
        //利用工具类 复制数据
            BeanUtils.copyProperties(activityWithBLOBs,activityDTO);
        //利用DateUtils将数据转化为string类型
        activityDTO.setBeginTime(DateUtils.longToStr(activityWithBLOBs.getBeginTime()));
        activityDTO.setEndTime(DateUtils.longToStr(activityWithBLOBs.getEndTime()));
        return activityDTO;
    }

    @Override
    public DataGridResult findByPage(QueryDTO queryDTO) {
        List list = new ArrayList();
        PageHelper.offsetPage(queryDTO.getOffset(),queryDTO.getLimit());
        ActivityExample example = new ActivityExample();
        if (!StringUtils.isEmpty(queryDTO.getSort())) {
            example.setOrderByClause("id");
        }
        List<ActivityWithBLOBs> activityWithBLOBs = activityMapper.selectByExampleWithBLOBs(example);
        //将获得的数据迭代，将迭代到的每个数据的时间转化为String类型，放到指定实体类zhong
        for (ActivityWithBLOBs activityWithBLOB : activityWithBLOBs) {
            //创建ActivityDTO ，接收数据
            ActivityDTO activityDTO = new ActivityDTO();
            //利用工具类 复制数据
                BeanUtils.copyProperties(activityWithBLOB,activityDTO);
            //利用DateUtils将数据转化为string类型
            activityDTO.setBeginTime(DateUtils.longToStr(activityWithBLOB.getBeginTime()));
            activityDTO.setEndTime(DateUtils.longToStr(activityWithBLOB.getEndTime()));
            list.add(activityDTO);
        }
        PageInfo pageInfo = new PageInfo(activityWithBLOBs);
        long total = pageInfo.getTotal();
        DataGridResult dataGridResult = new DataGridResult(total, list);
        return dataGridResult;
    }
}
